<template>
  <div>
    <base-list ref="processRule"
               :condition="condition"
               :datas.sync="processRule"
               :pageSearch="pageSearch">
      <el-form :inline="true"
               :model="condition"
               class="search-form"
               slot="search"
               label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="流程规则名称">
              <auto-search-process-rule @select="v=>condition['search_LIKE_ruleName']=v"></auto-search-process-rule>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属单位"
                          prop="orgId">
              <auto-search-org v-model="form.orgId"
                               @select="v=>{form.orgId=v;form.deptId='';form.deptName='';}"
                               @selectName="v=>form.orgName=v"
                               :disabled="true"
                               :name="form.orgName"></auto-search-org>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-button type="primary"
                       icon="el-icon-search"
                       @click="loadData"
                       class="ml-100">查 询</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-table slot="list"
                :data="processRule"
                border
                stripe
                max-height="500">
        <el-table-column label="序号"
                         min-width="50">
          <template slot-scope="scope">
            {{ scope.$index+1 }}
          </template>
        </el-table-column>
        <el-table-column prop="ruleName"
                         label="流程规则名称"
                         
                         min-width="130"></el-table-column>
        <el-table-column prop="orgName"
                         label="所属单位"
                         
                         min-width="200"></el-table-column>
        <el-table-column prop="businessModuleDesc"
                         label="业务模式"
                         
                         min-width="130"></el-table-column>
        <el-table-column prop="remark"
                         label="流程规则描述"
                         
                         min-width="120"></el-table-column>
        <el-table-column fixed="right"
                         align="center"
                         label="操作"
                         
                         width="140">
          <template slot-scope="scope">
            <el-button size="small"
                       type="text"
                       @click="handleDialogFormOpen(true,scope.row)">
              编辑
            </el-button>
            <el-button type="text"
                       class="danger"
                       size="small"
                       @click="handleRemove(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </base-list>
    <process-rule-add-form ref="processRuleAddForm"
                           @load="loadData"></process-rule-add-form>
    <div style="text-align:center">
      <el-button plain
                 type="info"
                 plain
                 @click="handleRouterPush({path: '/Home/SystemBase',name: '基础管理工作台'})">返回</el-button>
    </div>
  </div>
</template>

<script>
import {
  deleteProcessRule,
  getProcessRuleList
} from '@/api/processRule/processRule'
import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
import autoSearchDic from '@/components/autocomplete/autoSearchDic'
import autoSearchProcessRule from '@/components/autocomplete/autoSearchProcessRule'
import processRuleAddForm from './processRuleAddForm'

export default {
  name: 'processRule',
  components: {
    processRuleAddForm,
    autoSearchOrg,
    autoSearchDic,
    autoSearchProcessRule
  },
  data() {
    return {
      form: this.clearForm(),
      condition: {}, // 查询条件
      processRule: [], // table数组
      pageSearch: getProcessRuleList, // 分页查询api接口
      id: ''
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.loadData()
    })
  },
  methods: {
    clearForm() {
      return {
        orgId: '',
        orgId:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.id,
        orgName:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.name
      }
    },
    loadData() {
      this.$refs.processRule.loadData()
    },
    handleDialogFormOpen(bool, item) {
      if (bool) {
        this.$refs.processRuleAddForm.form = JSON.parse(JSON.stringify(item))
        this.$refs.processRuleAddForm.form.factors.forEach((item,index) => {
          item.attrName = ''
        })
        this.$refs.processRuleAddForm.isEdit = true
        this.$refs.processRuleAddForm.dialogForm = true
      } else {
        this.$refs.processRuleAddForm.dialogForm = true
      }
    },
    handleRemove(item) {
      this.$confirm(`是否删除该数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteProcessRule(item.id).then(res => {
            this.$message({
              type: 'success',
              message: `删除成功!`
            })
            this.loadData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    handleRouterPush(val) {
      this.$store.commit('ADD_CACHED_VIEWS', val)
      this.$router.push(val.path)
    }
  }
}
</script>

<style scoped>
</style>
